package com.cjj2016.cjj.mynestedscrolldemo.widget;

import android.content.Context;
import android.support.v4.view.NestedScrollingParent;
import android.support.v4.view.NestedScrollingParentHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cjj2016.cjj.mynestedscrolldemo.R;

/**
 * Created by CJJ on 2017/2/17.
 */

public class ElemeDetailView extends LinearLayout implements NestedScrollingParent {

    private static final String TAG = "ElemeDetailView";

    private TextView edv_title;
    private TextView edv_header;
    private LinearLayout edv_content;

    private int titleHeight,headHeight;

    private NestedScrollingParentHelper nestedScrollingParentHelper = new NestedScrollingParentHelper(this);

    private MyContentPostionListener mylistener;

    public interface MyContentPostionListener{
        void  onContentPositonChange(float fraction);
    }


    public ElemeDetailView(Context context) {
        super(context);
    }

    public ElemeDetailView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        edv_title = (TextView) findViewById(R.id.edv_title);
        edv_header = (TextView) findViewById(R.id.edv_header);
        edv_content = (LinearLayout) findViewById(R.id.edv_content);

        //监听edv_content的位置变化
        edv_content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if(mylistener != null){
                    //edv_content的移动范围为(titleHeight,titleHeight+headerHeight)
                    //据此算出一个百分比
                    float fraction = (edv_content.getY()-titleHeight) / headHeight;
                    mylistener.onContentPositonChange(fraction);
                }

                return true;
            }
        });

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        titleHeight = edv_title.getMeasuredHeight();
        headHeight = edv_header.getMeasuredHeight(); //也就是父view滚动的最大距离

        //重新测量ElemeDetailView高度
        int newheightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight()+headHeight,MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec,newheightMeasureSpec);

    }


    //移动edv_content
    private void offsetContent(int dy,int[] consumed){
        ViewCompat.offsetTopAndBottom(edv_content,-dy); //第二个参数为正代表向下，为负代表向上
        consumed[0]=0; //x轴消耗
        consumed[1] = dy;   //y轴消耗
    }



    public void setMylistener(MyContentPostionListener listener){
        mylistener = listener;
    }


    // 以下：NestedScrollingParent接口的实现--------------------

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        return true;
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {

        float supposeY =  edv_content.getY()-dy; //希望edv_content移动到的位置

        //向上滑动
        if(dy > 0 ){
            //edv_content的上边界为titleHeight
            if( supposeY >= titleHeight ){
                offsetContent(dy,consumed);
            }else{
                offsetContent( ((int)edv_content.getY() - titleHeight),consumed);
            }
        }else{
           //向下滑动

            if( !ViewCompat.canScrollVertically(target,dy)){ //当target不能再向下滑时，说明target已经滑动到了顶端
                //edv_content的下边界为titleHeight+headHeight
                if( supposeY <= (titleHeight+headHeight)){
                    offsetContent(dy,consumed);
                }else{
                    offsetContent( ((int)edv_content.getY() - titleHeight-headHeight), consumed );
                }

            }


        }

    }

    @Override
    public void onNestedScrollAccepted(View child, View target, int axes) {
        nestedScrollingParentHelper.onNestedScrollAccepted(child, target, axes);
    }

    @Override
    public void onStopNestedScroll(View child) {
        nestedScrollingParentHelper.onStopNestedScroll(child);
    }




    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {

        return false;
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        Log.i(TAG, "onNestedPreFling: velocityY="+velocityY);
        int offduration = (int) (velocityY * 0.1f);

        float supposeY =  edv_content.getY()-offduration; //希望edv_content移动到的位置
        //向上滑动
        if(offduration > 0 ){
            //edv_content的上边界为titleHeight
            if( supposeY >= titleHeight ){
                ViewCompat.offsetTopAndBottom(edv_content,-offduration);
                return true;
            }else{
                ViewCompat.offsetTopAndBottom(edv_content,-((int)edv_content.getY() - titleHeight));
            }
        }

        return false;

    }


    @Override
    public int getNestedScrollAxes() {
        return nestedScrollingParentHelper.getNestedScrollAxes();
    }


}
